#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#define ARGS_CHECK(argc, num)                 \
    {                                         \
        if (argc != num)                      \
        {                                     \
            fprintf(stderr, "args error!\n"); \
            return -1;                        \
        }                                     \
    }
#define ERROR_CHECK(ret, num, msg) \
    {                              \
        if (ret == num)            \
        {                          \
            perror(msg);           \
            exit(1);               \
        }                          \
    }
/**
 * @brief 
 * 
 * @param argc 
 * @param argv  ./  file   pipe  
 * @return int 
 */
int main(int argc, char *argv[])
{
    ARGS_CHECK(argc,3);
    int fddest = open(argv[1],O_WRONLY|O_TRUNC|O_CREAT,0664);
    ERROR_CHECK(-1,fddest,"open dest");
    int fdpipe = open(argv[2],O_RDONLY);
    ERROR_CHECK(-1,fdpipe,"open pipe");
    char r_buf[32] = {0};
    int ret = 0;
    while (ret =  read(fdpipe,r_buf,sizeof(r_buf)))
    {
          printf("%d\n",ret);
        puts(r_buf);
        write(fddest,r_buf,ret);
        memset(r_buf,0,sizeof(r_buf));
    }
       
      
      close(fddest);  
    close(fdpipe);  
    return 0;
}
